﻿using CollegeSim.CourseScheduler.Presentation.Facade;
using System;
using System.Windows.Forms;

namespace CollegeSim.CourseScheduler.Presentation
{
    // Author: Ryan Redburn
    // Project: Course Scheduler
    // Date: 2/16/2014

    #region [ Updates ]

    // Author: Ryan Redburn
    // Date: 4/6/2014
    // Revision: Wired up delete button and added comments.

    #endregion

    public partial class FormCourseManagement : Form
    {
        #region [ Fields ]

        private readonly SchedulerFacade _facade;

        #endregion

        #region [ Constructors ]

        public FormCourseManagement(SchedulerFacade facade)
        {
            InitializeComponent();
            _facade = facade;
        }

        #endregion

        #region [ Events ]

        // Displays course table on load
        private void FormViewCourses_Load(object sender, EventArgs e)
        {
            try
            {
                DisplayTable();
            }
            catch (InvalidOperationException x1)
            {
                MessageBox.Show(x1.Message);
            }
        }

        // Starts creation process
        private void btnCreate_Click(object sender, EventArgs e)
        {
            using (var frmCreate = new FormCreateCourse(_facade, false))
            {
                frmCreate.ShowDialog();
            }
        }

        // Starts update process
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            using (var frmUpdate = new FormCreateCourse(_facade, true))
            {
                frmUpdate.ShowDialog();
            }
        }

        // Starts deletion process
        private void btnDelete_Click(object sender, EventArgs e)
        {
            using (var frmDelete = new FormDeleteCourse(_facade))
            {
                frmDelete.ShowDialog();
            }
        }

        // Refreshes the course table
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            try
            {
                DisplayTable();
            }
            catch (InvalidOperationException x1)
            {
                MessageBox.Show(x1.Message);
            }
        }

        // Closes the form
        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        #endregion

        #region [ Methods ]

        // Sets the datagridview's data sourse to the data table retrieved from the facade
        private void DisplayTable()
        {
            dgvCourses.DataSource = _facade.GetCourseTable();
        }

        #endregion
    }
}
